﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Scheduling.Models.Interfaces;

namespace Scheduling.Models.Repos.InMemory
{
    public class InMemoryUserLoader : IUserLoader
    {

        private static List<User> UserList;


        public InMemoryUserLoader(){
            if (UserList == null)
            {
                UserList = new List<User>();

                User u1 = new User() { ID = "MBl", FirstName = "Martin", LastName = "Blom", Email = "martin.blom@example.com", Password = "5732d1258f7d39c7fbafbfce223b4b7cf25d99ccf52949bc15b31cc04e5c062e", Phone = "070-0707070" };
                User u2 = new User() { ID = "DFR", FirstName = "Donald", LastName = "Ross", Email = "donald.ross@example.com", Password = "5732d1258f7d39c7fbafbfce223b4b7cf25d99ccf52949bc15b31cc04e5c062e", Phone = "070-0101010" };

                u1.UserGroup = new UserGroup() { ID = 1, Name = "User" };
                u2.UserGroup = new UserGroup() { ID = 1, Name = "User" };
                u1.Department = new Department() { ID = 1, Name = "Fakulteten för teknik- och naturvetenskap" };
                u2.Department = new Department() { ID = 2, Name = "Fakulteten för teknik- och naturvetenskap" };
                UserList.Add(u1);
                UserList.Add(u2);
            }
        }

        public List<User> GetUsers()
        {


            return UserList;
        }


        public User GetUser(string UserID)
        {
            foreach (User user in UserList)
            {
                if (user.ID == UserID)
                {
                    return user;
                }
            }
            return null;
        }


        public bool UserExists(string UserID)
        {
            if (GetUser(UserID) == null)
                return false;
            else
                return true;
        }

        public void InsertUser(User user)
        {
            UserList.Add(user);
        }
        public void DeleteUser(string UserID)
        {
            for(int i=0;i<UserList.Count;i++)
            {
                if (UserList[i].ID == UserID)
                    UserList.RemoveAt(i);
            }
        }

        public void EditUser(User user)
        {
            DeleteUser(user.ID);
            InsertUser(user);
        }

        public void Clear()
        {
            UserList = null;
        }
    }
}